//
//  NinePicsModeView.m
//  XRmenu
//
//  Created by shengchao yang on 11-11-25.
//  Copyright 2011年 foxhis. All rights reserved.
//

#import "NinePicsModeView.h"
#import "NinePicsCell.h"
#import "PluPlu.h"

#define kTagOfTable 402
#define kTagXRGridViewBegin 1001

@implementation NinePicsModeView

-(id) initWithStyle:(NSString *)stylename DishSubViewName:(NSString *)vname
{
	self = [super initWithStyle:stylename DishSubViewName:vname];
	if (self) { 	
        [self addThumbView];
    }
    
    return self;
}
#pragma mark 数据更新
//=========================================================== 
// UpdateModeViewDataWithPluPlu
// 更新TableView模式显示的内容
// 通过DisplayModeView被MainView调用
//=========================================================== 
-(void) UpdateModeViewDataWithPluPlu{
    [((XRGridTableView *)[self viewWithTag:kTagOfTable]) reloadData];//滚动到当前页面
}
#pragma mark 业务实现部分
-(void) addThumbView
{
	while ([self viewWithTag:kTagOfStartTableView]) {
		[[self viewWithTag:kTagOfStartTableView] removeFromSuperview];
	}
	while ([self viewWithTag:kTagOfTable]) {
		[[self viewWithTag:kTagOfTable] removeFromSuperview];
	}
	XRGridTableView *tempTV =[[XRGridTableView alloc] initWithTarget:self position:CGPointMake(10,10)];
	[tempTV setTag:kTagOfTable];
	[self addSubview:tempTV];
	[tempTV release];
}

#pragma mark -
#pragma mark 多行多列table的 DataSouce & Delegate
//框大小
-(NSInteger) multiColTableViewShowRows
{
	return   3;
}
-(NSInteger) multiColTableViewShowColums
{
	return   3;
}
-(NSInteger) multiColTableViewDisplayCount
{
	return [APPSESSION.pluPlus count];
}

-(NinePicsCell*) multiColTableView:(XRGridTableView *)aTableView viewAtPosition:(NSInteger)aPosition
{
    NSDictionary *cfg = [APPSESSION.allconfig findAtPath:[NSString stringWithFormat:@"UI Skin/%@/Main View/Display Mode View/NinePics Mode View/Cell View", APPSESSION.uistyle]];
	CGRect frame =CGRectFromString([cfg getStringValueAtPath:@"view rect"]);

    NinePicsCell* cell = [[NinePicsCell alloc] initWithFrame:frame];
    [cell loadAllObjectByConfig:cfg];
	[cell configItem:aPosition];
	return [cell autorelease];
}
-(CGSize) cellSize
{
    NSString *c = [APPSESSION.allconfig getStringValueAtPath:[NSString stringWithFormat:@"UI Skin/%@/Main View/Display Mode View/NinePics Mode View/Cell View/view rect", APPSESSION.uistyle]];
	CGRect frame =CGRectFromString(c);
    return frame.size;
}
-(CGSize) cellMargin
{
    NSString *c = [APPSESSION.allconfig getStringValueAtPath:[NSString stringWithFormat:@"UI Skin/%@/Main View/Display Mode View/NinePics Mode View/Cell View/view margin", APPSESSION.uistyle]];
	CGRect frame =CGRectFromString(c);
    return frame.size;
}

-(void) multiColTableView:(XRGridTableView *)aTableView didTappedAtPosition:(NSInteger)aPosition withCell:(XRGridTableCell*)aCell
{	
    [APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"buttonClicked.caf"]];
        
//        //方案一：选中菜品显示两个按钮
//        NSInteger lastPageIndex = APPSESSION.lastPluIndex / 9;//上个菜品的页数
//        NSInteger lastCurrentIndex = APPSESSION.lastPluIndex % 9;//上个菜品所在的index
//        DebugLog(@"lastPageIndex=%d,lastCurrentIndex=%d", lastPageIndex,lastCurrentIndex);
//        DebugLog(@"currentPageIndex=%d", aTableView.currentPageIndex);
//        
//        //1.去掉上一个cell的选中状态
//        if (lastPageIndex==aTableView.currentPageIndex && [self viewWithTag:kTagXRGridViewBegin + lastCurrentIndex]) {
//            NinePicsCell *lastCell=(NinePicsCell *)[self viewWithTag:kTagXRGridViewBegin + lastCurrentIndex];
//            [lastCell setSelectedCell:NO];
//        }
//        //  设置当前cell的选中状态
//        NinePicsCell *selectedCell=(NinePicsCell *)aCell;
//        [selectedCell setSelectedCell:YES];
    
    //方案二：选中菜品直接显示大图详细信息
    //1.修改当前菜品的下标位置
    [APPSESSION updateCurrentPluIndex:aPosition];
    //2.显示大图详细信息
    postN(kMessage_RootView_PhotoPopoverViewDisplay);
    //3.通知主界面修改右边tableview的选中状态
    postN(kMessage_MainView_SelectMainViewPluPlu);
}

#pragma mark 隐藏式tableview的 Datasource & Delegate
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [APPSESSION.pluPlus count];
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}
@end
